package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.PageResult;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.entity.Advimg;
import com.itheima.ydd.entity.Advpos;
import com.itheima.ydd.service.AdvimgService;
import com.itheima.ydd.service.AdvposService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 广告位管理
 */
@Slf4j
@RestController
@RequestMapping("ydd_advpos")
public class AdvposController {
    @Autowired
    private AdvposService advposService;
    @Autowired
    private AdvimgService advimgService;

    /**
     * 新增广告
     *
     * @param advpos
     * @return
     */
    @PostMapping("/add")
    public ResultData add(@RequestBody Advpos advpos) {
        log.info("新增广告为:{}", advpos.toString());
        Advpos advposa = advpos;
        advposa.setDefault_data("1");
        advposService.save(advposa);
        log.info(advposa.toString());
        return ResultData.ok("", " ");
    }

    /**
     * 分页查询
     *
     * @param advposdesc
     * @param advposname
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/list")
    public ResultData list(String advposdesc, String advposname, Integer page, Integer limit) {
        log.info("数据为:{}", advposdesc, advposname, page, limit);
        IPage<Advpos> pageInfo = new Page<>(page, limit);

        //条件构造器
        LambdaQueryWrapper<Advpos> queryWrapper = new LambdaQueryWrapper<>();
        //添加条件

        queryWrapper.like(StringUtils.isNotEmpty(advposdesc), Advpos::getAdvposdesc, advposdesc);
        queryWrapper.like(StringUtils.isNotEmpty(advposname), Advpos::getAdvposname, advposname);
        //添加排序条件
        queryWrapper.orderByDesc(Advpos::getId);
        advposService.page(pageInfo, queryWrapper);

        pageInfo.getRecords().stream().map((item) -> {
            LambdaQueryWrapper<Advimg> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(Advimg::getAdvimgpos, item.getId());
            int count = advimgService.count(queryWrapper1);
            item.setAdvposnum(count);
            return item;

        }).collect(Collectors.toList());

        PageResult init = PageResult.init(pageInfo.getTotal(), pageInfo.getSize(), pageInfo.getPages(), pageInfo.getRecords());
        return ResultData.ok("", init);


    }

    @GetMapping("/del")
    public ResultData del(@RequestParam Long id) {
        log.info("删除的id为:{}", id);
        advposService.removeById(id);
        return ResultData.ok("", "成功");
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public ResultData rowInfo(@RequestParam Long id) {
        log.info("查询Id为:{}", id);
        Advpos advpos = advposService.getById(id);

        return ResultData.ok("", advpos);
    }

    /**
     * 修改方法
     *
     * @param advpos
     * @return
     */
    @PostMapping("/update")
    public ResultData update(@RequestBody Advpos advpos) {
        log.info("修改Id为:{}", advpos);
        advposService.updateById(advpos);

        return ResultData.ok("", "成功");
    }

    @GetMapping("/all")
    public ResultData all() {
        List<Advpos> list = advposService.list();


        return ResultData.ok("", list);
    }
}
 